﻿using System.ComponentModel.DataAnnotations;
using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Tip_newspaper
{
    public class TransplantingSeachHandler: IRequestHandler<TransplantingSeachCommon, Result>
    {
        public readonly IBaseRepository<Descendent> DescendentRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public TransplantingSeachHandler(IBaseRepository<Descendent> descendentRepository, IBaseRepository<Columns> columnsRepository, IBaseRepository<Building> buildingRepository, IBaseRepository<BasicInformation> basicInformationRepository)
        {
            DescendentRepository = descendentRepository;
            ColumnsRepository = columnsRepository;
            BuildingRepository = buildingRepository;
            BasicInformationRepository = basicInformationRepository;
        }

        public async Task<Result> Handle(TransplantingSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                res.Code = (int)CommonEnumCode.success;
                var list = (from a in DescendentRepository.GetAll()
                            join b in ColumnsRepository.GetAll()
                            on a.ColumnsEnter equals b.ColumnsId
                            join c in BuildingRepository.GetAll()
                            on a.BuildingsEnter equals c.BuildingId
                            join d in BasicInformationRepository.GetAll()
                            on a.breedingSheepArchiveId equals d.BreedingSheepArchiveId
                            select new DescendentDTO
                            {
                                DescendentId=a.DescendentId,
                                TransplantingDate= a.TransplantingDate,
                                Auricle=d.Auricle,
                                BEnter = c.BuildingName,
                                CEnter = b.ColumnsName,
                                Building = BuildingRepository.GetAll().Where(x => x.BuildingId == a.BuildingId).FirstOrDefault().BuildingName,
                               
                                Columns = ColumnsRepository.GetAll().Where(x => x.ColumnsId == a.ColumnsId).FirstOrDefault().ColumnsName,
                                Sex =a.Sex,
                                Age=a.Age,
                            }).ToList();
                if (!string.IsNullOrEmpty(request.TransplantingDate))
                {
                    list = list.Where(x => x.TransplantingDate >= DateTime.Parse(request.TransplantingDate)).ToList();
                }
                if (!string.IsNullOrEmpty(request.JTransplantingDate))
                {
                    list = list.Where(x => x.TransplantingDate <= DateTime.Parse(request.JTransplantingDate)).ToList();
                }
                if (request.BreedingSheepArchiveId > 0)
                {
                    list = list.Where(x => x.BreedingSheepArchiveId == request.BreedingSheepArchiveId).ToList();
                }
                res.Code = (int)CommonEnumCode.success;
                res.Data = list;
                res.Msg = "后裔转种显示";
            }
            catch (Exception)
            {

                throw;
            }
            return res;
        }
    }
    public class DescendentDTO
    {
        /// <summary>
        /// 后裔转种主键
        /// </summary>
        [Key]
        public int DescendentId { get; set; }
        /// <summary>
        /// 育成羊耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 耳号名称
        /// </summary>
        public string Auricle { get; set; }
        /// <summary>
        /// 转种日期
        /// </summary>
        public DateTime TransplantingDate { get; set; }
        /// <summary>
        /// 转入栋舍id
        /// </summary>
        public int BuildingsEnter { get; set; }
        /// <summary>
        /// 转入栋舍名称
        /// </summary>
        public string BEnter { get; set; }
        /// <summary>
        /// 转入栏位id
        /// </summary>
        public int ColumnsEnter { get; set; }
        /// <summary>
        /// 转入栏位名称
        /// </summary>
        public string CEnter { get; set; }
        /// <summary>
        /// 当前栋舍
        /// </summary>
        public int BuildingId { get; set; }
        /// <summary>
        /// 当前栋舍名称
        /// </summary>
        public string Building { get; set; }
        /// <summary>
        /// 当前栏位
        /// </summary>
        public int ColumnsId { get; set; }
        /// <summary>
        /// 当前栏位名称
        /// </summary>
        public string Columns { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public int Sex { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public string Age { get; set; }
    }
}
